<?php
    require_once __DIR__ . '/dumpHelper.php';

    function formatTraceback(array $traceback, $contextLines=10) {
        $result = array();
        $result[] = '<div>';
        foreach ($traceback as $index=>$traceItem) {
            $id = uniqid();
            $result[] = '<div class="file">';
            $result[] = '<noscript>';
            $result[] = '   <span class="filename">' . $traceItem['file'] . '</span>:<span class="line">' . $traceItem['line'] . '</span>';
            $result[] = '</noscript>';
            $result[] = '<script type="text/javascript">';
            $result[] = '   document.write(\'<a href="#" onclick="toggleTraceback(this, this.parentNode.getElementsByTagName(\\\'div\\\')[0]); return false;">' . $traceItem['file'] . ':' . $traceItem['line'] . '&nbsp;&' . ($index > 0 ? '#x25B6;' : '#x25BC;') . '</a>\');';
            $result[] = '</script>';
            $lines = explode("\n", file_get_contents($traceItem['file']));
            $offset = $traceItem['line'] - round($contextLines/2);
            if ($offset < 0) {
                $offset = 0;
            }
            if ($offset+$contextLines > count($lines)) {
                $offset = count($lines) - $contextLines;
            }
            $result[] = '<div class="context" id="' . $id . '">';
            for ($idx = $offset; $idx < $offset + $contextLines; $idx++) {
                $line = $lines[$idx];
                $result[] = '<pre class="row' . ($idx + 1 == $traceItem['line'] ? ' active' : '') . '">' . ($idx + 1) . '.' . htmlspecialchars(rtrim($line, "\n")) . '</pre>';
            }
            $result[] = '</div>';
            if ($index > 0) {
                $result[] = '<script type="text/javascript"> document.getElementById(\'' . $id . '\').style.display = \'none\'; </script>';
            }
            $result[] = '</div>';
        }
        $result[] = '</div>';
        return implode("\n", $result);
    }
?>
<script type="text/javascript">
    function toggleTraceback(caller, id, cssDisplay) {
        var el = null;
        if (typeof(id) == 'string') {
            el = document.getElementById(id);
        } else {
            el = id;
        }
        el.style.display = el.style.display == 'none' ? (cssDisplay ? cssDisplay : 'block') : 'none';
        if (el.style.display != 'none') {
            caller.innerHTML = caller.innerHTML.replace(String.fromCharCode(9654), String.fromCharCode(9660));
            Dom.addClass(caller.parentNode.parentNode, 'expanded');
        } else {
            caller.innerHTML = caller.innerHTML.replace(String.fromCharCode(9660), String.fromCharCode(9654));
            Dom.removeClass(caller.parentNode.parentNode, 'expanded');
        }
    }

    function filter(caller) {
        var type = caller.options[caller.selectedIndex].value;
        var elems = document.getElementById('messages').getElementsByTagName('tr');
        for (var i = 1; i < elems.length; i++) {
            if ((type == 'all' || Dom.hasClass(elems[i], type)) && !Dom.hasClass(elems[i], 'traceback')) {
                elems[i].style.display = 'table-row';
            } else {
                elems[i].style.display = 'none';
            }

            if (Dom.hasClass(elems[i], 'traceback') && elems[i-1]) {
                if (Dom.hasClass(elems[i-1], 'expanded') && elems[i-1].style.display != 'none') {
                    elems[i].style.display = 'table-row';
                }
            }
        }
    }
</script>

<h2><?php echo $data->get('title'); ?></h2>
<div id="messages">
    <?php if (count($data->get('messages', '/', array())) > 0): ?>
    <script type="text/javascript">
        document.write('<label>Filter</label>');
        document.write('<select onchange="filter(this)">');
        document.write('<option value="all">all</option>');
        <?php
            $types = array();
            foreach ($data->get('messages') as $message):
                $types[] = $message->getType();
            endforeach;
            $types = array_unique($types);
            sort($types);
            foreach ($types as $type):
                echo "document.write('<option value=\"" . $type . "\">" . $type . "</option>');";
            endforeach;
        ?>
        document.write('</select>');
    </script>
    <table>
        <tr>
            <th>No.</th>
            <th>Type</th>
            <th>Message</th>
            <th>Additional info</th>
        </tr>
        <?php foreach ($data->get('messages') as $index=>$message): ?>
        <tr class="<?php echo $message->getType(); ?>">
            <td><?php echo $index+1; ?>.</td>
            <td><?php echo $message->getType(); ?></td>
            <td><?php echo htmlspecialchars($message); ?></td>
            <td><?php
                switch ($message->getType()) {
                    case 'error':                        
                        echo '<script type="text/javascript">';
                        echo 'document.write(\'<a href="#" onclick="toggleTraceback(this, this.parentNode.parentNode.nextSibling.nextSibling, \\\'table-row\\\'); return false;">traceback&nbsp;&#x25B6;</a>\');';
                        echo '</script>';
                    break;
                    case 'time':
                    case 'query':
                        echo \DebugComponent\Renderer\Humanize::time($message->getTimeInSecond());
                    break;
                    case 'memory':
                        echo \DebugComponent\Renderer\Humanize::capacity($message->getMemoryInKB()*1024);
                    break;
                    case 'file':
                        echo $message->getFilename();
                    break;
                    case 'dump':
                        echo dump($message->getVariable());
                    break;
                }
            ?></td>
        </tr>
        <?php if ($message->getType() == 'error'): ?>
        <tr class="traceback">
            <td colspan="4" class="traceback"><?php echo formatTraceback($message->getTraceback()); ?></td>
        </tr>
        <script type="text/javascript">
            var rows = document.getElementById('messages').getElementsByTagName('tr');
            rows[rows.length-1].style.display = 'none';
        </script>
        <?php endif; ?>
        <?php endforeach; ?>
    </table>
    <?php else: ?>
    <span class="noMessages">No messages</span>
    <?php endif; ?>
</div>